package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.EasybuyProduct;
import com.ly.bean.EasybuySeckillGoods;
import com.ly.mapper.EasybuyProductMapper;
import com.ly.mapper.EasybuySeckillGoodsMapper;
import com.ly.service.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
@author 李岩
@date 2021/12/14-13:52
@describe
*/
@Component
@Service(interfaceClass = SeckillGoodsService.class)
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private EasybuySeckillGoodsMapper easybuySeckillGoodsMapper;
    @Autowired
    private EasybuyProductMapper productMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        int count = 0;
        try {
            count = easybuySeckillGoodsMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int insert(EasybuySeckillGoods record) {
        int count = 0;
        try {
            count = easybuySeckillGoodsMapper.insert(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int insertSelective(EasybuySeckillGoods record) {
        int count = 0;
        try {
            count = easybuySeckillGoodsMapper.insertSelective(record);
            //管理员审核通过以后把商品表里面的 库存减去抢购的数量
            productMapper.updateProductStatusByPid(record.getNum(),record.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public EasybuySeckillGoods selectByPrimaryKey(Integer id) throws Exception{
        EasybuySeckillGoods easybuySeckillGoods =easybuySeckillGoods = easybuySeckillGoodsMapper.selectByPrimaryKey(id);

        return easybuySeckillGoods;
    }
    /**
     * 根据商品id查询该商品信息
     */
    @Override
    public EasybuySeckillGoods selectKillGoodFromRedis(Integer id) throws Exception {
        EasybuySeckillGoods killGoods =(EasybuySeckillGoods) redisTemplate.boundHashOps("killGoods").get(id+"");
        return killGoods;
    }

    @Override
    public void updateByPrimaryKeySelective(EasybuySeckillGoods record) {
        try {
            easybuySeckillGoodsMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateByPrimaryKey(EasybuySeckillGoods record) {
        try {
            easybuySeckillGoodsMapper.updateByPrimaryKey(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //查询所有的秒杀商品的申请
    @Override
    public PageInfo<EasybuySeckillGoods> showAllKillProduct(Integer pageNum) throws Exception {
        Integer pageNumInt=1;
        Integer pageSizeInt=5;
        if (pageNum!=null){
            pageNumInt=pageNum;
        }
        PageHelper.startPage(pageNumInt,pageSizeInt);
        List<EasybuySeckillGoods> list = easybuySeckillGoodsMapper.showAllKillProduct();
        System.out.println("length="+list.size());
        PageInfo<EasybuySeckillGoods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //查询首页显示的秒杀商品
    @Override
    public List<EasybuySeckillGoods> showSixKillProduct() throws Exception {

        List<EasybuySeckillGoods> seckillGoods =(List<EasybuySeckillGoods>) redisTemplate.boundHashOps("killGoods").values();
        //如果redis不存在  那就从数据库中查询 然后 放到reids上
        if (seckillGoods==null || seckillGoods.size()==0){
            seckillGoods = easybuySeckillGoodsMapper.showSixKillProduct();
            for (EasybuySeckillGoods seckillGood : seckillGoods) {
                redisTemplate.boundHashOps("killGoods").put(seckillGood.getId()+"",seckillGood);
            }
        }

        return seckillGoods;
    }

}
